﻿using System;
using System.Collections.Generic;
using System.Drawing;

namespace DestopIcon.Utility
{
    class ExeIcon
    {
        public static extern int ExtractIconEx(string lpszFile, int niconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, int nIcons);

        public static List<Icon> ExtractIcon(string fileName)
        {
            int i;
            var large = (IntPtr)0;
            var small = (IntPtr)0;
            var icons = new List<Icon>();
            int nIcons = ExtractIconEx(fileName, -1, ref large, ref small, 1);

            for (i = 0; i < nIcons; i++)
            {
                ExtractIconEx(fileName, i, ref large, ref small, 1);
                icons.Add(Icon.FromHandle(large));
            }

            return icons;
        }
    }
}
